package com.example.boot3.rpc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class test {

    public static class User {
        private Long id;
        private String name;
        private Integer age;
        private Boolean isStudent;

        // 构造方法
        public User() {}

        public User(Long id, String name, Integer age, Boolean isStudent) {
            this.id = id;
            this.name = name;
            this.age = age;
            this.isStudent = isStudent;
        }

        // Getter和Setter
        public Long getId() { return id; }
        public void setId(Long id) { this.id = id; }

        public String getName() { return name; }
        public void setName(String name) { this.name = name; }

        public Integer getAge() { return age; }
        public void setAge(Integer age) { this.age = age; }

        public Boolean getIsStudent() { return isStudent; }
        public void setIsStudent(Boolean isStudent) { this.isStudent = isStudent; }

        @Override
        public String toString() {
            return "User{id=" + id + ", name='" + name + "', age=" + age +
                    ", isStudent=" + isStudent + "}";
        }
    }

    public class FastJsonConversion {

        public static void main(String[] args) {
            // 创建Java对象
            User user = new User(1L, "张三", 25, false);

            // 方法1: Java对象 → JSONObject (最常用)
            JSONObject jsonObject1 = (JSONObject) JSON.toJSON(user);
            System.out.println("方法1 - JSONObject: " + jsonObject1);

            // 方法2: 通过JSON字符串中转
            String jsonString = JSON.toJSONString(user);
            JSONObject jsonObject2 = JSON.parseObject(jsonString);
            System.out.println("方法2 - JSONObject: " + jsonObject2);

            // 方法3: 直接构造JSONObject
            JSONObject jsonObject3 = new JSONObject();
            jsonObject3.put("id", user.getId());
            jsonObject3.put("name", user.getName());
            jsonObject3.put("age", user.getAge());
            jsonObject3.put("isStudent", user.getIsStudent());
            System.out.println("方法3 - JSONObject: " + jsonObject3);
        }
    }
}
